package com.sun.eureka.controller.api;


import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.sun.eureka.entry.Customer;

@RestController
public class CustomerController {
	
	@Autowired
    RestTemplate restTemplate;
	
	@HystrixCommand(fallbackMethod = "errorGet",
			commandKey="getCustomer",
			groupKey="customerServer",
			threadPoolKey="customerThreadPookey",
			commandProperties= {@HystrixProperty(name="execution.isolation.strategy",value="THREAD"),
	},
			threadPoolProperties = {
		            @HystrixProperty(name = "coreSize",value = "2"),
		            @HystrixProperty(name = "maximumSize",value="4"),
		            @HystrixProperty(name = "maxQueueSize",value="5"),
		            @HystrixProperty(name = "queueSizeRejectionThreshold",value="2")

		    }
	)
	@RequestMapping("/customer/{id}")
	public Customer get(@PathVariable Long id) throws InterruptedException {
		
		return restTemplate.getForEntity("http://micro-customer-server/customer/"+id,Customer.class).getBody();
	}
	
	public Customer errorGet(Long id) {
		//TimeUnit.MINUTES.sleep(1);
        return new Customer(0l,null,null,null);
    }


}
